#include "widegraph.h"
#include "ui_widegraph.h"

#define NFFT 32768

WideGraph::WideGraph(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::WideGraph)
{
  ui->setupUi(this);
  this->setWindowFlags(Qt::Dialog);
  this->installEventFilter(parent); //Installing the filter
  ui->widePlot->setWide(true);
  ui->widePlot->setBinsPerPixel(17);

  //Restore user's settings
  QString inifile(QApplication::applicationDirPath());
  inifile += "/map65.ini";
  QSettings settings(inifile, QSettings::IniFormat);

  settings.beginGroup("WideGraph");
  ui->widePlot->setPlotZero(settings.value("PlotZero", 0).toInt());
  ui->widePlot->setPlotGain(settings.value("PlotGain", 0).toInt());
  ui->zeroSpinBox->setValue(ui->widePlot->getPlotZero());
  ui->gainSpinBox->setValue(ui->widePlot->getPlotGain());
  ui->freqSpanSpinBox->setValue(settings.value("FreqSpan",0).toInt());
  m_waterfallAvg = settings.value("WaterfallAvg",0).toInt();
  ui->waterfallAvgSpinBox->setValue(m_waterfallAvg);
  ui->freqOffsetSpinBox->setValue(settings.value("FreqOffset",0).toInt());
  settings.endGroup();
}

WideGraph::~WideGraph()
{
  //Save user's settings
  QString inifile(QApplication::applicationDirPath());
  inifile += "/map65.ini";
  QSettings settings(inifile, QSettings::IniFormat);

  settings.beginGroup("WideGraph");
  settings.setValue("PlotZero",ui->widePlot->m_plotZero);
  settings.setValue("PlotGain",ui->widePlot->m_plotGain);
//  settings.setValue("PlotWidth",ui->widePlot->plotWidth());
  settings.setValue("FreqSpan",ui->freqSpanSpinBox->value());
  settings.setValue("WaterfallAvg",ui->waterfallAvgSpinBox->value());
  settings.setValue("FreqOffset",ui->widePlot->freqOffset());
  settings.endGroup();
  delete ui;
}

void WideGraph::dataSink2(float s[], int nkhz)
{
  static float savg[NFFT];
  float sw[2048];
  float smax;
  double df = 96000.0/32768.0;
  int nbpp = ui->widePlot->binsPerPixel();
  static int n=0;
  static int nkhz0=-999;

  //Average spectra over specified number, m_waterfallAvg
  if (n==0) {
    for (int i=0; i<NFFT; i++)
      savg[i]=s[i];
  } else {
    for (int i=0; i<NFFT; i++)
      savg[i] += s[i];
  }
  n++;

  if (n>=m_waterfallAvg) {
    //Normalize the average
    for (int i=0; i<NFFT; i++)
        savg[i] /= n;
    n=0;

    ui->widePlot->SetCenterFreq(nkhz);
    if(nkhz != nkhz0) {
      ui->widePlot->setFQSO(nkhz,true);
      nkhz0 = nkhz;
    }
    int w=ui->widePlot->plotWidth();

//    qDebug() << "widegraph:" << ui->widePlot->m_Span << nbpp;

    qint64 sf = nkhz + ui->widePlot->freqOffset() - \
        0.5*w*nbpp*df/1000.0;
    if(sf != ui->widePlot->startFreq()) ui->widePlot->SetStartFreq(sf);
    int i0=16384.0+(ui->widePlot->startFreq()-nkhz+1.27046)*1000.0/df + 0.5;
    int i=i0;
    for (int j=0; j<2048; j++) {
        smax=0;
        for (int k=0; k<nbpp; k++) {
            i++;
            if(savg[i]>smax) smax=savg[i];
        }
        sw[j]=smax;
    }
    ui->widePlot->draw(sw);
  }
}

//--------------------------------------------------------------------------
// on_freqOffsetSpinBox_valueChanged
//--------------------------------------------------------------------------
void WideGraph::on_freqOffsetSpinBox_valueChanged(int f)
{
  ui->widePlot->SetFreqOffset(f);
}

//--------------------------------------------------------------------------
// on_freqSpanSpinBox_valueChanged
//--------------------------------------------------------------------------
void WideGraph::on_freqSpanSpinBox_valueChanged(int n)
{
//  need something to limit value of nbpp depending on w
  int w = ui->widePlot->plotWidth();
  int nbpp = n * 32768.0/(w*96.0) + 0.5;
  if(nbpp < 1) nbpp=1;
//  if(nbpp > 20) nbpp=20;
//  qDebug() << "B" << w << nbpp << ui->widePlot->m_Span;
  if(w > 0) {
    ui->widePlot->setBinsPerPixel(nbpp);
  }
}

//--------------------------------------------------------------------------
// on_waterfallAvg_valueChanged
//--------------------------------------------------------------------------
void WideGraph::on_waterfallAvgSpinBox_valueChanged(int n)
{
  m_waterfallAvg = n;
}

void WideGraph::on_zeroSpinBox_valueChanged(int value)
{
  ui->widePlot->setPlotZero(value);
}

void WideGraph::on_gainSpinBox_valueChanged(int value)
{
  ui->widePlot->setPlotGain(value);
}

void WideGraph::mousePressEvent(QMouseEvent *event)
{
  int n=event->x();
  ui->widePlot->setFQSO(n,false);
}

void WideGraph::keyPressEvent(QKeyEvent *e)
{
  e->ignore();
}

